package com.haocang.szgm.ui.home.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.haocang.szgm.R;
import com.haocang.szgm.activity.comm.CommFinalActivity;
import com.haocang.szgm.requestcenter.DataModel;
import com.haocang.commonlib.network.interfaces.InitDataCallBack;
import com.haocang.szgm.ui.home.adapter.WeatherAdapter;
import com.haocang.szgm.ui.home.bean.WeatherBean;
import com.haocang.szgm.utils.config.Config;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.annotation.view.ViewInject;

import org.json.JSONObject;

import java.util.List;

public class WeatherActivity extends CommFinalActivity  {

    @ViewInject(id = R.id.iv_home_weather)
    ImageView iv_home_weather;
    @ViewInject(id = R.id.tv_home_weather_wd)
    TextView tv_home_weather_wd;
    @ViewInject(id = R.id.tv_home_weather_memo)
    TextView tv_home_weather_memo;
    @ViewInject(id = R.id.tv_home_weather_memo1)
    TextView tv_home_weather_memo1;
    @ViewInject(id = R.id.tv_home_weather_pubdate)
    TextView tv_home_weather_pubdate;
    @ViewInject(id = R.id.iv_home_weather_close)
    ImageView iv_home_weather_close;
    @ViewInject(id = R.id.list)
    ListView list;
    private WeatherAdapter mAdapter;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        initView();

        initEvent();

        initData();
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_weather;
    }

    @Override
    protected boolean showNavigation() {
        return false;
    }

    @Override
    protected int showMoreIcon() {
        return 0;
    }

    @Override
    protected String showTitleName() {
        return "天气预报";
    }

    private void initView(){


    }

    private void initEvent(){

        iv_home_weather_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });

    }

    private void initData(){


        DataModel.GetApiRequest(context, Config.SZWeatherURL, new InitDataCallBack() {
            @Override
            public void callbak(boolean b, String string) {

                if (b) {
                    try {
                        JSONObject jsondata = new JSONObject(string);
                        if(jsondata.getInt("code") == 200) {
                            JSONObject jsonroot = new JSONObject(jsondata.getString("data"));
                            JSONObject json = new JSONObject(jsonroot.getString("data"));
                            String imgurl = jsonroot.getString("imgpre") + json.getString("img");
                            String pubdate = jsonroot.getString("pubdate");//发布时间
                            String mint = json.getString("mint");//最低温度
                            String maxt = json.getString("maxt");//最高温度
                            String report = json.getString("report");//描述
                            String trend = json.getString("trend");//未来天气预报描述
                            tv_home_weather_wd.setText(mint + "℃~" + maxt + "℃");
                            tv_home_weather_memo.setText(report);
                            tv_home_weather_memo1.setText(trend);
                            tv_home_weather_pubdate.setText(pubdate+"发布");

                            FinalBitmap fb = FinalBitmap.create(context);
                            fb.display(iv_home_weather, imgurl);

                            Gson gson = new Gson();
                            JSONObject json_10d = new JSONObject(json.getString("forecast10d"));
                            List<WeatherBean> datalist = gson.fromJson(json_10d.getString("list"), new TypeToken<List<WeatherBean>>() {
                            }.getType());
                            mAdapter = new WeatherAdapter(context,jsonroot.getString("imgpre"));
                            list.setAdapter(mAdapter);
                            mAdapter.setList(datalist);
                        }else{
                            ToastShow("天气获取失败," + jsondata.getString("info"));
                        }

                    } catch (Exception e) {
                        ToastShow("天气数据解析失败," + e.toString());
                    }
                } else {
                    ToastShow("天气获取失败," + string);
                }
            }
        });

    }

    //退出程序
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            return super.onKeyDown(keyCode, event);
        }else{
            return  false;
        }
    }

}
